package resp

import (
	"go-project/common/base/req"
	"net/http"

	"github.com/gin-gonic/gin"
)

type Response struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
	Data any    `json:"data"`
}

type PageData struct {
	PageIndex int   `json:"pageIndex"`
	PageSize  int   `json:"pageSize"`
	List      any   `json:"list"`
	Total     int64 `json:"total"`
}

func Ok(c *gin.Context) {
	c.JSON(http.StatusOK, &Response{Code: 200, Msg: "OK"})
}

func OkWith(c *gin.Context, data any) {
	c.JSON(http.StatusOK, &Response{Code: 200, Msg: "OK", Data: data})
}

func OkWithPage(c *gin.Context, page req.Pager, data any, count int64) {
	c.JSON(http.StatusOK, &Response{Code: 200, Msg: "OK", Data: &PageData{
		page.PageIndex, page.PageSize, data, count,
	}})
}

func OkWithMsg(c *gin.Context, msg string) {
	c.JSON(http.StatusOK, &Response{Code: 200, Msg: msg})
}

func BadRequest(c *gin.Context, msg string) {
	c.JSON(http.StatusBadRequest, &Response{Code: 400, Msg: msg})
}

func Error(c *gin.Context, msg string) {
	c.JSON(http.StatusInternalServerError, &Response{Code: 500, Msg: msg})
}

func AuthError(c *gin.Context, msg string) {
	c.JSON(http.StatusUnauthorized, &Response{Code: 401, Msg: msg})
}

func Data(c *gin.Context, data any, err error) {
	if err != nil {
		Error(c, err.Error())
	} else {
		OkWith(c, data)
	}
}

func Page(c *gin.Context, page req.Pager, data any, count int64, err error) {
	if err != nil {
		Error(c, err.Error())
	} else {
		OkWithPage(c, page, data, count)
	}
}
